<!-- zTree -->
<SCRIPT type='text/javascript' src="${base}/js/ztree/js/jquery.ztree.core-3.4.min.js"></SCRIPT>
<SCRIPT type='text/javascript' src="${base}/js/ztree/js/jquery.ztree.exedit-3.4.min.js"></SCRIPT>
<link rel="stylesheet" type="text/css" href="${base}/js/ztree/css/zTreeStyle/zTreeStyle.css">
<!-- dialog -->
<link rel="stylesheet" type="text/css" href="${base}/js/dialog/dialog.css">
<link rel="stylesheet" type="text/css" href="${base}/js/dialog/productdialog.css">
<SCRIPT type='text/javascript' src="${base}/js/dialog/dialog.js"></SCRIPT>
<!-- dwr -->
<script type='text/javascript' src='${base}/dwr/engine.js'></script>
<script type='text/javascript' src='${base}/dwr/util.js'></script>
<script type='text/javascript' src='${base}/dwr/interface/backPurviewService.js'></script>
<script type="text/javascript">
	j(function(){
		j.fn.zTree.init(j("#ztreeul"), setting, zNodes);
		j("#expandAllBtn").bind("click", {type:"expandAll"}, expandNode);
		j("#collapseAllBtn").bind("click", {type:"collapseAll"}, expandNode);
	})
		var setting = {
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pId",
					rootPId: 0
				}
			},
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom
			}
		};
	
	var zNodes =[
	    <#if bpLevelIs?exists && bpLevelIs?size gt 0>
	    	<#list bpLevelIs as bpLevelI>
	    		{ id:${bpLevelI.id?if_exists},pId:${bpLevelI.parent?default(0)},pLevel:"${bpLevelI.level?if_exists}",name:"${bpLevelI.name} (No.${bpLevelI.sortNo?if_exists?c})",open:true,isParent:true,icon:"${base}/images/3.gif"}<#if bpLevelI_has_next>,<#else><#if bpLevelIIs?exists && bpLevelIIs?size gt 0>,</#if></#if>
	    	</#list>
			
	    	<#list bpLevelIIs as bpLevelII>
	    		{ id:${bpLevelII.id?if_exists},pId:${bpLevelII.parent.id?default(0)},pLevel:"${bpLevelII.level?if_exists}",name:"${bpLevelII.name} (No.${bpLevelII.sortNo?if_exists?c})",open:true,icon:"${base}/images/11.gif"}<#if bpLevelII_has_next>,</#if>
	    	</#list>
		</#if>
	];
	
	function expandNode(e) {
		var zTree = j.fn.zTree.getZTreeObj("ztreeul"),
		type = e.data.type,
		nodes = zTree.getSelectedNodes();
		if (type.indexOf("All")<0 && nodes.length == 0) {
			alert("请先选择一个父节点");
		}

		if (type == "expandAll") {
			zTree.expandAll(true);
		} else if (type == "collapseAll") {
			zTree.expandAll(false);
		} else {
			var callbackFlag = $("#callbackTrigger").attr("checked");
			for (var i=0, l=nodes.length; i<l; i++) {
				zTree.setting.view.fontCss = {};
				if (type == "expand") {
					zTree.expandNode(nodes[i], true, null, null, callbackFlag);
				} else if (type == "collapse") {
					zTree.expandNode(nodes[i], false, null, null, callbackFlag);
				} else if (type == "toggle") {
					zTree.expandNode(nodes[i], null, null, null, callbackFlag);
				} else if (type == "expandSon") {
					zTree.expandNode(nodes[i], true, true, null, callbackFlag);
				} else if (type == "collapseSon") {
					zTree.expandNode(nodes[i], false, true, null, callbackFlag);
				}
			}
		}
	}
	
	function addHoverDom(treeId, treeNode) {
		var aObj = j("#" + treeNode.tId + "_a");
		if (j("#cyEdit_"+treeNode.id).length>0) return;
		var editStr=""
		+"<span style='' treenode_edit='' title='添加权限节点' id='cyAdd_" +treeNode.id+"'  class='button add'></span>"
		+"<span style='' treenode_edit='' title='修改权限节点' id='cyEdit_"+treeNode.id+"'  class='button edit'></span>"
		+"<span style='' treenode_edit='' title='删除权限节点' id='cyDel_" +treeNode.id+"'  class='button remove'></span>"
		aObj.append(editStr);
		var addPurview = j("#cyAdd_"+treeNode.id);
		if (addPurview) addPurview.bind("click",function(){treeControl(treeNode,"ADD")});
		var editPurview = j("#cyEdit_"+treeNode.id);
		if (editPurview) editPurview.bind("click", function(){treeControl(treeNode,"EDIT")});
		var delPurview = j("#cyDel_"+treeNode.id);
		if (delPurview) delPurview.bind("click", function(){treeControl(treeNode,"DEL")});
	};
	function removeHoverDom(treeId, treeNode) {
		j("#cyAdd_"+treeNode.id).unbind().remove();
		j("#cyDel_"+treeNode.id).unbind().remove();
		j("#cyEdit_"+treeNode.id).unbind().remove();
	};
	
	function treeControl(treeNode,control){
		if(control=="ADD"){
			if(parseInt(treeNode.pLevel) >= 1){
				alert("权限级联最高2级");
				return;
			}
			dialog("新增权限","iframe:${base}/go/addNewPurview.action?backPurview.level="+(parseInt(treeNode.pLevel)+1)+"&parentPurviewId="+treeNode.id+"&parentPurviewName="+treeNode.name,"500px","250px","iframe");
		}else if(control=="EDIT"){
			dialog("编辑权限","iframe:${base}/go/editNewPurview.action?backPurviewId="+treeNode.id,"500px","250px","iframe");
		}else if(control=="DEL"){
			if(typeof(treeNode.children)!="undefined"){
				if(confirm(treeNode.name+"\n此节点为父级权限节点,会级联删除子权限节点")){
					backPurviewService.delBackPurviewDWR(treeNode.id,true,function(){addSuccess("backPurviewManage.action")})
				}else{
					return;
				}
				
			}else{
				if(confirm(treeNode.name+"\n确认要删除此节点吗")){
					backPurviewService.delBackPurviewDWR(treeNode.id,false,function(){addSuccess("backPurviewManage.action")})
				}else{
					return;
				}
			}
		}
	}
	
	function addNewPurview(){
		dialog("新增顶级权限","iframe:${base}/go/addNewPurview.action?backPurview.level=0&parentPurviewName=顶级权限","500px","250px","iframe");
	}
	
	function addSuccess(url){
		alert("修改成功");
		setTimeout(function(){ window.location.href=url },1000)
	}
</script>
<div class="container">
	<div class="masage_tittle">
		<h3>权限列表管理&nbsp;&nbsp;&nbsp;<span style="color: red;font-size: 12px">(序号越小排位越前)</span></h3>
	</div>	<br>
	<input type="button" value="新增顶级权限"  onclick="addNewPurview()"/>&nbsp;&nbsp;&nbsp;<input type="button" value="全部展开" id="expandAllBtn"/>&nbsp;<input type="button" value="全部收缩" id="collapseAllBtn"/>
	<br><br>
	
	<div>
		<ul id="ztreeul" class="ztree"></ul>
	</div>




</div>	